package me.mononin.dilechat.fragments.media;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.BottomSheetDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import me.mononin.common.common.app.Fragment;
import me.mononin.common.common.tools.UiTool;
import me.mononin.common.common.widget.GalleryView;
import me.mononin.dilechat.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class GalleryFragment extends BottomSheetDialogFragment implements
        GalleryView.SelectedChangeListener{


    private GalleryView mGallery;
    private OnSelectedListener mListener;

    public GalleryFragment() {

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new BottomSheetDialog(getContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_gallery, container, false);
        mGallery = root.findViewById(R.id.gallery_view);
        return root;
    }

    /**
     * 初始化图片选择控件
     */
    @Override
    public void onStart() {
        super.onStart();
        mGallery.setup(getLoaderManager(),this);
    }

    @Override
    public void onSelectedCountChanged(int count) {
        //选中了一张图片
        if (count>0){
            //将Fragment隐藏起来
            dismiss();
            if (mListener !=null){
                //获取图片所有路径
                String[] path = mGallery.getSelectedPath();
                //执行监听方法
                mListener.onSelected(path[0]);
                //执行口清空监听方法，取消联系
                mListener = null;
            }
        }
    }

    public GalleryFragment setListener(OnSelectedListener listener){
        this.mListener = listener;
        return this;
    }

    /**
     * 图片选中时监听接口
     */
    public interface OnSelectedListener{
        void onSelected(String path);
    }


    public static class TranStatusBottomSheetDialog extends BottomSheetDialog{

        public TranStatusBottomSheetDialog(@NonNull Context context) {
            super(context);
        }

        public TranStatusBottomSheetDialog(@NonNull Context context, int theme) {
            super(context, theme);
        }

        protected TranStatusBottomSheetDialog(@NonNull Context context, boolean cancelable, OnCancelListener cancelListener) {
            super(context, cancelable, cancelListener);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            final Window window = getWindow();
            //获取屏幕高度
            int screenHeight = UiTool.getScreenHeight(getOwnerActivity());
            //获取状态栏高度
            int statusHeight = UiTool.getStatusBarHeight(getOwnerActivity());
            //获取Dialog实际高度
            int dialogHeight = screenHeight - statusHeight;


            //设置Dialog实际高度
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                    dialogHeight<=0?ViewGroup.LayoutParams.MATCH_PARENT:dialogHeight);
        }
    }
}
